package com.fu.jdbc05;

import com.fu.jdbc05.domain.Student;
import com.fu.jdbc05.handler.BeanHandler;
import com.fu.jdbc05.handler.BeanListHandler;
import com.fu.jdbc05.handler.ScalarHandler;
import com.fu.utils.DataSourceUtils;
import org.junit.Test;

import java.util.List;

/**
 * @author 付大大
 * @description
 * @create 2021-01-31 16:27
 */
public class JDBCTemplateTest1 {

    private JDBCTemplate template = new JDBCTemplate(DataSourceUtils.getDataSource());

    @Test
    public void insert() {
        String sql = "insert into student values (?,?,?,?)";
        Object[] params = {5, "周期", 27, "1997-07-07"};
        int update = template.update(sql, params);
        if (update != 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    @Test
    public void update() {
        String sql = "update student set age=? where name=?";
        Object[] params = {37,"周期"};
        int result = template.update(sql, params);
        System.out.println(result);
    }

    @Test
    public void delete() {
        String sql = "delete from student where name=?";
        int result = template.update(sql, "周期");
        System.out.println(result);
    }


    @Test
    public void queryForObject() {
        String sql = "select * from student where sid=?";
        Student student = template.queryForObject(sql, new BeanHandler<>(Student.class), 1);
        System.out.println(student);
    }


    @Test
    public void queryForList() {
        String sql = "select * from student";
        List<Student> students = template.queryForList(sql, new BeanListHandler<>(Student.class));
        for (Student student : students) {
            System.out.println(student);
        }
    }


    @Test
    public void queryForScalar() {
        String sql = "select count(*) from student";
        Long value = template.queryForScalar(sql, new ScalarHandler<Long>());
        System.out.println(value);
    }




}
